import { observable, action } from 'mobx';
import * as  Apis from '../apis/govern';
import {WeaTools} from 'ecCom';
import isEqual from "lodash/isEqual";
import isEmpty from "lodash/isEmpty";
import { message, Modal } from "antd";
import { ListStore } from './listStore';
import WeaUtils from "../components/E9/dialog/wea-utils";
import { WeaLocaleProvider } from "ecCom";
import GetFormJson from '../util/getFormJson';
import _ from "lodash";
const getLabel = WeaLocaleProvider.getLabel;
const { GetRegex, DateFormat } = WeaUtils;
var Base64 = ecCom.WeaTools.Base64;

export default class CheckMatterStore  extends ListStore{
    ////
    @observable tasklabels = {};
    @observable condition = [];
    @observable conditionNum = [];
    @observable inputOnFocus = false;
    @observable taskId = "";
    @observable instype = "";
    @observable pageindex = 1;
    @observable pagesize = 10;
    @observable hasChild = false;
    @observable
    status = {
        title: getLabel(26468, "我的微博"),
        loading: false,
        userInfo: {},
        weiboGroup: {},
        weiboCKEditorConfig: {},//富文本相关设置
        //currentParams: {}, //当前搜索条件,用于判断返回的数据是否为搜索数据
        basicInfo: {},
        receiverList: {},
        weiboList: {},
        attentionList: {},
        messageList: {},
        note: {},
        defaultTemplate: {},
        WeaRichTextStoreValue: {},
        richInit: true
      };
      @observable userList = [];
      constructor(props) {
          super(props);
          this.getInstype = this.getInstype.bind(this);
          this.getPageInfo = this.getPageInfo.bind(this);
          this.isEndPage = this.isEndPage.bind(this);
      }
    //获取任务类型
    @action
    getTaskId(id){
          this.taskId = id;
    }
    @action
    getInstype = (instype)=>{
      this.instype = instype;
    }
    @action
    getPageInfo(params){
        this.pageindex = params["pageindex"];
        this.pagesize = params["pagesize"];
        this.init(params);
    }
    //是否是最后一页
    @action
    isEndPage(){
        return (this.pageindex == ( Math.ceil(this.status.weiboList.total/this.pagesize) )?true:false)
    }
    //获取Condition
    @action
    getCheckCondition(params){
        this.status.loading = true;
        this.resetForm();
        Apis.getProInfo(params).then(data=>{
          if(parseInt(data.status) == 1){
            this.hasChild = data.hasChild;
            this.maininfoTitle = data.title;
            let formData = _.cloneDeep(data);
            if(parseInt(formData.status) == 1){
                this.mackCondition(formData);
            }else{
              message.error(formData.error);
            }
          }else{
            message.error(data.error);
          }
          this.status.loading = false;
      }).catch(error => {
          message.error(error);
      });
    }
    @action
    mackCondition(formData){
        let f =  _.cloneDeep(formData);
        let dbFormData = _.has(f,'condition1') ? _.pick(f, ['condition1']):[];
        let rwFormData = _.has(f,'conditions') ? _.pick(f,['conditions']):[];
        if( dbFormData != []) this.setDbInfo(dbFormData);//督办阶段
        if( rwFormData != []) this.setRwInfo(rwFormData);//任务阶段
    }
    //构造新的dbFormData.condition1
    setDbInfo(dbFormData){
      if(_.has(dbFormData,'condition1')){
        dbFormData.condition1 = dbFormData.condition1 || [];
         dbFormData.condition1.map((c)=>{
             let getData = [];
             if(_.has(c,'items')){
               c.items = c.items || [];
               c.items.map((item,index)=>{
                   if(item.domkey[0] == "remark" || item.domkey[0] == "fujian"){
                     item={
                       ...item,
                       colSpan: 1,
                       labelcol: 0,
                       fieldcol: 24
                      };
                      getData.push(item);
                      if(item.domkey[0] == "remark" && item.value === ""){
                        getData.splice(index,1);
                      }
                 }else{
                   item={
                     ...item,
                     colSpan: 2,
                     labelcol: 7,
                     fieldcol: 17
                   };
                   getData.push(item);
                 }

               });
               c.items = [];
               c.items = c.items.concat(getData);
             }
         });
         this.condition = dbFormData.condition1;
         this.form && !this.form.isFormInit && this.form.initFormFields(dbFormData.condition1);
     }
    }
    //构造新的rwFormData.conditions
    setRwInfo(rwFormData){
        if(_.has(rwFormData,'conditions')){
           rwFormData.conditions = rwFormData.conditions || [];
            rwFormData.conditions.map((c,index)=>{
                let _str = index;
                let getData = [];
                if(_.has(c,'items')){
                  c.items = c.items || [];
                  c.items.map((item,index)=>{
                      let  itemDomkey = item.domkey[0].replace(`_${_str}`, '');
                      if(itemDomkey == "remark" || item.domkey[0] == "name"){
                        item={
                          ...item,
                          colSpan: 1,
                          labelcol: 3,
                          fieldcol: 21
                        };
                        getData.push(item);
                        if(itemDomkey == "remark" && item.value === ""){
                          getData.splice(index,1);
                        }
                    }else{
                      item={
                        ...item,
                        colSpan: 3,
                        labelcol: 10,
                        fieldcol: 14
                      };
                      getData.push(item);
                    }

                  });
                  c.items = [];
                  c.items = c.items.concat(getData);
                }
            });
            this.conditionNum = rwFormData.conditions;
            this.formB && !this.formB.isFormInit && this.formB.initFormFields(rwFormData.conditions);
        }
    }
    //存储富文本数据
    saveWeaRichTextValue = (objs, para = true) => {
        let WeaRichTextStoreValue = {
          ...this.status.WeaRichTextStoreValue,
          ...objs
        };
        this.status = {
          ...this.status,
          WeaRichTextStoreValue: WeaRichTextStoreValue,
          richInit: para
        };
      };
  getUserList(params) {
    return this.userList.slice();
  }
//初始化
@action
  init = (params) => {
     Apis.getInstructList(params).then(data => {
          if(parseInt(data.status) == 1){
            let result = [
                  {
                      "appList":[
                          {
                              "items":[
                                  "Document",
                                  "Schedule",
                                  "Flow"
                                  /* "Task",
                                  "Templet" */
                              ],
                              "name":"dialogs"
                          },
                          {
                              "items":[
                                  {
                                      "Attachment":{
                                          "uploadUrl":"/api/doc/upload/uploadFile?model=reply",
                                          "position":"/api/doc/upload/uploadFile?model=reply",
                                          "maxSize":"256"
                                      }
                                  }
                              ],
                              "name":"uploads"
                          }
                      ],
                      "status":"1"
                  }
            ];
          result = result.concat(data);
          //解码评论内容
          let copyresult1 = { ...result[1] };
          copyresult1.discussList && copyresult1.discussList.forEach((record, index) => {
            if (record.content) {
              let decodeContent = Base64.decode(record.content);
              result[1].discussList[index] = {
                ...record,
                content: decodeContent
              };
            }
            if (record.instructReplyList && record.instructReplyList.length > 0) {
              record.instructReplyList.forEach((_record, _index) => {
                let decodeContentReply = Base64.decode(_record.content);
                result[1].discussList[index].instructReplyList[_index] = {
                  ..._record,
                  content: decodeContentReply
                };
              });
            }
          });
          this.status = {
            ...this.status,
            weiboCKEditorConfig: result[0],
            weiboList: result[1],
            loading: false
          };
          }else{
            message.error(data.error);
          }
     }).catch(error => {
         message.error(error);
     });
    
  };
  //发布
    editWeibo = (params,otherparams,callback = false) => {
        //ok
        if (params.content) {
          //message.loading(getLabel('384534',"正在提交中..."), 10);
          params.content = params.content.replace(GetRegex("wrap"), "");
          Apis.saveInstruct(params)
          .then(result => {
              if (result.status == "1") {
                message.loading(getLabel('384534',"正在提交中..."), 20);
                message.destroy();
                message.success(getLabel('83885',"操作成功！"));
                //() => callback(result);
                callback?callback(result):null
                this.init(otherparams);
              } else if(data.status == -3){
                message.warning(data.error)
                return ;
            }else {
                message.destroy();
                message.error(getLabel('384535',"提交失败！"));
              }
            })
            .catch(error => {
              message.warning(getLabel('501166',"输入内容过多了"));
              return true;
            });
        } else {
          Modal.warning({ title: getLabel(127354,"警告"), content: getLabel('126982',"请填写内容") });
          //Apis.getNull();
        }
      };
      //删除
      deleteComment = (params,otherparams) => {
        Apis.deleteInstruct(params)
          .then(result => {
            if(parseInt(result.status) == 1){
              message.success(getLabel('83885',"操作成功！"));
              this.init(otherparams);
            }else{
              message.error(result.error);
            }
            // let copyresult = { ...result };
            // copyresult.replyList.forEach((record, index) => {
            //   if (record.content) {
            //     let decodeContent = Base64.decode(record.content);
            //     result.replyList[index] = { ...record, content: decodeContent };
            //   }
            // });
          }).catch(error => {
            message.warning(error);
          });
      }
}
